package sugar;

/**
 *  NonStaticInitializer: A nonstatic initializer block will be called any time a constructor is invoked (just prior to invoking the constructor). While this is a valid language construct, it is rarely used and is confusing.

 翻译  非静态的初始化器：非静态的初始化块将在构造器被调用的时候被访问(优先于调用构造器)。这是一个有效的语言结构，但使用很少且易造成迷惑。
 */
public class Beef1 {
    public static void main(String[] args){
        aaa(new NonStaticInitializerChild(){

            {
//                System.out.println("childCode");
                x = 3;
//                System.out.println(x);
            }

            @Override
            public void setX() {
//                System.out.println("Override");
                super.setX();
            }
        });
    }

    public static void aaa(NonStaticInitializerChild nonStaticInitializerChild){
        System.out.println(nonStaticInitializerChild.x);
    }
}

class NonStaticInitializer{
    public int x = 1;
    public NonStaticInitializer(){
        System.out.println("construct");
        System.out.println(x);
    }
    {
        System.out.println("myCode");
    }
    static {
        System.out.println("staticCode");
    }
    public void setX(){
        x = 2;
    }
}

class NonStaticInitializerChild extends NonStaticInitializer{
    public int x = 1;
    public NonStaticInitializerChild(){
//        super();
        System.out.println("constructChild");
        System.out.println(x);
    }
    {
        System.out.println("myCodeChild");
    }
    static {
        System.out.println("staticCodeChild");
    }
    public void setX(){
        x = 2;
    }
}